package com.weiqiao.mall.rest;

import com.weiqiao.aop.log.Log;

import com.weiqiao.mall.domain.CommodityEntity;
import com.weiqiao.mall.service.ICommodityListService;
import com.weiqiao.mall.service.ICommodityService;
import com.weiqiao.utils.PageUtil;
import com.weiqiao.utils.SecurityUtils;
import com.weiqiao.utils.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/api")
public class ScoreController {

    @Autowired
    private ICommodityListService commodityListService;
    
    @Log("获取积分商品")
    @GetMapping("/get-score-commodity-list")
    public ResponseEntity getScoreCommodityList(HttpServletRequest request, Pageable pageable) {
        int company_id = SecurityUtils.getCompanyId();
        //组装查询条件
        Document filter = this.getCommodityFilter(request);
        int recordCount = commodityListService.countCommodity(company_id, filter);
        List<CommodityEntity> List = commodityListService.findCommoditys(company_id, filter, pageable);
        return new ResponseEntity(PageUtil.toPage(List, recordCount), HttpStatus.OK);
    }

    private Document getCommodityFilter(HttpServletRequest request) {
        Document doc = new Document("delete_flag", 0).append("state", 0);
        String name = request.getParameter("name");
        String is_score_commodity = request.getParameter("is_score_commodity");
        // 为积分商品
        // 查询条件不为空
        if (StringUtils.isNotBlank(name)) {
            Pattern pattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
            doc.append("name", pattern);
        }
        // 查询是否是积分商品
        if (StringUtils.isInteger(is_score_commodity)) {
            doc.append("is_score_commodity", Integer.parseInt(is_score_commodity));
        }
        return doc;
    }

    /**
     * 新增，编辑，删除积分商品
     * 描述：因为积分商品只是商品身上的一个属性，且is_score_commodity为 1 时为积分。所以，在删除积分商品时，只需要全部改为0就好了
     * @param entity 商品实体
     * @return
     */
    @PostMapping("/edit-score-commodity")
    public ResponseEntity updateScoreCommodity(@RequestBody CommodityEntity entity) {
        //组装查询条件
        Bson filter = new Document("company_id", entity.getCompany_id()).append("commodity_id", entity.getCommodity_id());
        // 更新内容
        Document doc = new Document();
        doc.append("is_score_commodity", entity.getIs_score_commodity());
        doc.append("score_type", entity.getScore_type());
        doc.append("sell_score", entity.getSell_score());
        return new ResponseEntity(commodityListService.updateState(entity.getCompany_id(), filter, doc), HttpStatus.OK);
    }
}
